<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let random = Math.floor(Math.random() * 51)
      console.log(random);
      let flag = true
      for(let i = 1;i<=8;i++){
        let num = +prompt('请输入一个0-50的整数，有8次机会')
        // 进行判断，true使代码能够进入到switch判断中
        switch (true) {
          case num > random:
            alert('猜大了')
            break
          case num < random:
            alert('猜小了')
            break
          case num === random: //当猜对时，flag变为false
            alert('恭喜你猜对了')
            flag = false
            break
        }
        if(flag === false){
          break
        }
      }
      // 当flag为true时代码为for循环完成值未改变
      if(flag === true){
        alert('8次用完了')
      }
    </script>
  </body>
</html>
